package com.xuexiang.xuidemo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.j256.ormlite.stmt.query.In;
import com.xuexiang.xui.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.widget.imageview.ImageLoader;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.app.DataProvider;
import com.xuexiang.xuidemo.app.activity.TestStartPage;
import com.xuexiang.xuidemo.app.activity.TestingPage;
import com.xuexiang.xuidemo.utils.XToastUtils;

public class WordListAdapter extends BaseRecyclerAdapter<WordListItem> implements View.OnClickListener {

    private Context context;

    private OnSelectionChangedListener mOnSelectionChangedListener;

    public WordListAdapter(Context context) {
        super();
        this.context = context;
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.app_adapter_word_list_item;
    }

    public WordListAdapter setOnSelectionChangedListener(OnSelectionChangedListener onSelectionChangedListener) {
        mOnSelectionChangedListener = onSelectionChangedListener;
        return this;
    }

    @Override
    protected void bindData(@NonNull RecyclerViewHolder holder, int position, WordListItem item) {
        if (item == null) {
            return;
        }
        holder.text(R.id.word_name, item.word);
        RadiusImageView imageView = holder.findViewById(R.id.word_image);

        View layout = holder.findView(R.id.word_item);
        layout.setOnClickListener(this);
        ImageLoader.get().loadImage(imageView, item.image);
    }

    @Override
    public void onClick(View v) {
        TextView textView = v.findViewById(R.id.word_name);
        CharSequence text = textView.getText();
        startTest(text.toString());
        XToastUtils.toast("view click" + text);
    }

    private void startTest(String word) {
        Intent intent = new Intent(context, TestingPage.class);
        intent.putExtra("word", word);
        context.startActivity(intent);
    }

    /**
     * 选择发生变化的监听
     */
    public interface OnSelectionChangedListener {
        /**
         * 选择发生变化
         *
         * @param item
         */
        void onSelectionChanged(WordListItem item);
    }


}

